<?php
namespace app\forum\model;
use app\base\model\BaseModel;
/**
 * 边栏操作
 */
class SidebarModel extends BaseModel {
    //完成
    protected $_auto = array (
        array('name','htmlspecialchars',3,'function'),
        array('sidebar_id','intval',2,'function'),
     );
    //验证
    protected $_validate = array(
        array('name','1,200', '边栏名称只能为1~200个字符', 1 ,'length'),
        array('sidebar_id','require', '边栏ID获取不正确', 1 ,'regex',2),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array()){
        return $this->where($where)->order('sequence asc')->select();
    }

    /**
     * 获取边栏数量
     * @return array 列表
     */
    public function countList($where = array()){
        return $this->where($where)->count();
    }

    /**
     * 获取边栏模块
     * @return array 列表
     */
    public function getModel(){
        $list = get_all_service('Sidebar','Forum');
        if(empty($list)){
            return $list;
        }
        $data = array();
        foreach ($list as $key => $vo) {
            if(empty($vo)){
                continue;
            }
            foreach ($vo as $k => $v) {
                $data[$key.'_'.$k] = $v;
            }
        }
        return $data;
    }
    /**
     * 获取信息
     * @param int $sidebarId ID
     * @return array 信息
     */
    public function getInfo($sidebarId)
    {
        $map = array();
        $map['sidebar_id'] = $sidebarId;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
        return $this->where($where)->find();
    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        $data = $this->create();
        if(!$data){
            return false;
        }
        if(!$data['type']){
            $data['content'] = request('post.diy');
        }else{
            $data['content'] = request('post.model');
        }
        if($type == 'add'){
            return $this->add($data);
        }
        if($type == 'edit'){
            if(empty($data['sidebar_id'])){
                return false;
            }
            $status = $this->save();
            if($status === false){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 删除信息
     * @param int $sidebarId ID
     * @return bool 删除状态
     */
    public function delData($sidebarId)
    {
        $map = array();
        $map['sidebar_id'] = $sidebarId;
        return $this->where($map)->delete();
    }

}
